package com.pzsh.jwt;

import com.pzsh.constant.ConstantInterface;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;
import java.util.Map;

@Component
public class JwtUtil {


    /**
     * 由字符串生成加密key
     *
     * @return
     */
    public  SecretKey generalKey() {
        String stringKey = ConstantInterface.JWT_KEY + ConstantInterface.JWT_SECRET;
        byte[] encodedKey = Base64.decodeBase64(stringKey);
        SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
        return key;
    }

    /**
     * 创建jwt
     *
     * @param userId    用户ID
     * @param account    声明
     * @param claims     自定义参数
     * @param ttlMillis 过期时间
     * @return
     * @throws Exception
     */
    public  String createJWT(long userId, String account, Map<String,Object> claims, long ttlMillis) throws Exception {
        //The JWT signature algorithm we will be using to sign the token
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);

        //We will sign our JWT with our ApiKey secret
        SecretKey signingKey = generalKey();

        //Let's set the JWT Claims
        JwtBuilder builder = Jwts.builder().setId(account)
                .setIssuedAt(now)
                .setSubject("")
                .setIssuer(ConstantInterface.JWT_ISSUER)
                .setAudience(account)
                .signWith(signatureAlgorithm, signingKey);

        // add claims
        builder.addClaims(claims);

        //if it has been specified, let's add the expiration

        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);
        }

        //Builds the JWT and serializes it to a compact, URL-safe string
        return builder.compact();
    }

    /**
     * 解密jwt
     *
     * @param jwt
     * @return
     * @throws Exception
     */
    public Claims parseJWT(String jwt) throws Exception {
        SecretKey key = generalKey();
        Claims claims = Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(jwt).getBody();
        return claims;
    }

}
